package com.j180.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.j180.bean.EmpBean;
import com.j180.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//@Controller
@RequestMapping("/ajax")
//@ResponseBody
@RestController //restFul风格的
public class AjaxController {
    @Autowired
    private IEmpService iEmpService;
     @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping("/one")
    public void ajax1(HttpServletResponse response){

       List<EmpBean> list =   iEmpService.showAllEmpsInfo();

        try {
            String json = objectMapper.writeValueAsString(list);
            response.getWriter().print(json);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/two")
   // @ResponseBody // 该注解的作用就时把  返回的对象自动转成json字符串
    public  List<EmpBean> getAllEmps(){

        System.out.println("getAllEmps---->>>controller");
        List<EmpBean> list =   iEmpService.showAllEmpsInfo();
        return list;
    }

    @RequestMapping("/getEmpInfoById")
    //@ResponseBody
    public EmpBean getEmpInf(@RequestParam("eid") Long id){

       return   iEmpService.showEmp(id);
    }

}


